package com.epower.web.action.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowerwordforday.EpowerWordForDayService;
import com.epower.dataobject.EpowerWordForDayDO;

/**
 * @author bolin.hbc
 * 
 *         每日一词的获取接口
 * 
 */
@Scope("prototype")
@Component("wordday")
public class WordDayAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String showtime;
	private Result result = Result.succResult();

	private EpowerWordForDayService epowerWordForDayBO;

	public String doGetWordsByTime() {
		Date time = null;
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		if (showtime == null || showtime.isEmpty()) {
			try {
				time = new Date(new java.util.Date().getTime());
			} catch (Exception e) {

				return ERROR;
			}
		} else {
			try {
				time = new Date(sim.parse(showtime).getTime());
			} catch (ParseException e) {
				return ERROR;
			}
		}
		EpowerWordForDayDO wordForDay = new EpowerWordForDayDO();
		wordForDay.setShowtime(time);
		List<EpowerWordForDayDO> wordForDayList = epowerWordForDayBO.getEpowerWordForDayListByOption(wordForDay);

		List<TmpWordDay> tmps = new ArrayList<TmpWordDay>();
		for (EpowerWordForDayDO wd : wordForDayList) {
			TmpWordDay tmp = new TmpWordDay();
			tmp.setSpeeling(wd.getSpelling());
			tmp.setContent(wd.getContent());
			tmp.setRank(wd.getRank());
			tmp.setRating(wd.getRating());
			tmp.setShowtime(wd.getShowtime().getTime());
			tmp.setGroupId(wd.getGroupid());
			tmp.setRoot(wd.getRoot());
			tmps.add(tmp);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("words", tmps);
		result.setResult(resultMap);
		return SUCCESS;

	}

	public class TmpWordDay {

		private String speeling;

		private String content;

		private long showtime;

		private Integer rank;

		private Integer rating;

		private Integer groupId;

		private String root;

		
		public Integer getGroupId() {
			return groupId;
		}

		public void setGroupId(Integer groupId) {
			this.groupId = groupId;
		}

		public String getRoot() {
			return root;
		}

		public void setRoot(String root) {
			this.root = root;
		}
		
        public String getSpeeling() {
            return speeling;
        }

        public void setSpeeling(String speeling) {
            this.speeling = speeling;
        }

        public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public long getShowtime() {
			return showtime;
		}

		public void setShowtime(long showtime) {
			this.showtime = showtime;
		}

		public Integer getRank() {
			return rank;
		}

		public void setRank(Integer rank) {
			this.rank = rank;
		}

		public Integer getRating() {
			return rating;
		}

		public void setRating(Integer rating) {
			this.rating = rating;
		}

	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public void setEpowerWordForDayBO(EpowerWordForDayService epowerWordForDayBO) {
		this.epowerWordForDayBO = epowerWordForDayBO;
	}

	public String getShowtime() {
		return showtime;
	}

	public void setShowtime(String showtime) {
		this.showtime = showtime;
	}

}
